/* eventUtil v1.0 2017-07-19 @fhluo */
/* 跨游览器的事件对象 */
var EventUtil = {
	addHandler: function(element, type, handler){
		if (element.addEventListener){
			element.addEventListener(type, handler, false);
		}else if(element.attachEvent){
			element.attachEvent('on'+type, handler);
		}else{
			element['on'+type] = handler;
		}
	},
	getEvent: function(event){
		return event ? event : window.event;
	},
	getTarget: function(event){
		return event.target || event.srcElement;
	},
	preventDefault: function(event){
		if (event.preventDefault){
			event.preventDefault();
		}else{
			event.returnValue = false;
		}
	},
	removeHandler: function(element, type, handler){
		if (element.removeEventListener){
			element.removeEventListener(type, handler, false);
		}else if(element.detachEvent){
			element.detachEvent('on'+type, handler);
		}else{
			element['on'+type] = null;
		}
	},
	stopPropagation: function(event){
		if (event.stopPropagation){
			event.stopPropagetion();
		}else{
			event.cancelBubble = true;
		}
	},
	getRelatedTarget: function(event) {
		if(event.relatedTarget){
			return event.relatedTarget;
		}else if(event.toElement){
			return event.toElement;
		}else if(event.fromElement){
			return event.fromElement;
		}else{
			return null;
		}
	},
	getCharCode: function(event){
		if(typeof event.charCode == 'number'){
			return event.charCode;
		}else{
			return event.keyCode;
		}
	}
};
/* 方法返回元素在页面中相对于视口的位置 */
function getBoundingClientRect(element){
	var scrollTop = document.documentElement.scrollTop;
	var scrollLeft = document.documentElement.scrollLeft;
	if(element.getBoundingClientRect){
		if(typeof arguments.callee.offset != 'number'){
			var scrollTop = document.documentElement.scrollTop;
			var temp = document.createElement('div');
			temp.style.cssText = 'position: absolute; left: 0; top: 0;';
			document.body.appendChild(temp);
			arguments.callee.offset = -temp.getBoundingClientRect().top - scrollTop;
			document.body.removeChild(temp);
			temp = null;
		}
		var rect = element.getBoundingClientRect();
		var offset = arguments.callee.offset;
		return{
			left: rect.left + offset,
			right: rect.right + offset,
			top: rect.top + offset,
			bottom: rect.bottom + offset
		};
	}else{
		var actualLeft = getElementLeft(element);
		var actualTop = getElementTop(element);
		return{
			left: actualLeft - scrollLeft,
			right: actualLeft + element.offsetWidth - scrollLeft,
			top: actualTop - scroolTop,
			bottom: actualTop + element.offsetHeight - scrollTop
		}
	}
}
/* 判断是否为pc端或移动端 */
function isPC(){
	var userAgentInfo = navigator.userAgent;
	var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = false;
			break;
		}
	}
	return flag;
}
/* 方法返回元素在页面上的偏移量 */
function getElementLeft(element){
	var actualLeft = element.offsetLeft;
	var actualTop = element.offsetTop;
	var current = element.offsetParent;
	while(current !== null){
		actualLeft += current.offsetLeft;
		actualTop += current.offsetTop;
		current = current.offsetParent;
	}
	return{
		actualLeft: actualLeft,
		actualTop: actualTop
	};
}
/* 方法返回元素内容及其内边距所占据的空间大小 */
function getViewport(){
	if(document.compatMode == 'BackCompat'){
		return{
			width: document.body.clientWidth,
			height: document.body.clientHeight
		};
	}else{
		return{
			width: document.documentElement.clientWidth,
			height: document.documentElement.clientHeight
		};
	}
}
/* 滚动条置顶 */
function scrollToTop(element){
	if(element.scrollTop != 0){
		element.scrollTop = 0;
	}
}
/* 去除文本节点 */
function delTextNode(elem){
	var elem_child = elem.childNodes;
		for(var i=0; i<elem_child.length;i++){
		if(elem_child[i].nodeType == 3 && !/\s/.test(elem_child.nodeValue)){
			elem.removeChild(elem_child[i]);
		}
	}
}
/* 鼠标在页面的坐标位置 */
function getMousePos(event) {
    var e = EventUtil.getEvent(event);
    var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
    var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
    var x, y;
    if(isPC()){
    	x = e.pageX || e.clientX + scrollX;
    	y = e.pageY || e.clientY + scrollY;
    }else{
    	x = e.changedTouches[0].pageX || e.changedTouches[0].clientX + scrollX;
    	y = e.changedTouches[0].pageY || e.changedTouches[0].clientY + scrollY;
    }
    return {
    	pageX: x,
    	pageY: y
    };
}
/* 鼠标在屏幕的坐标位置 */
function getScreenMousePos(event){
	event = EventUtil.getEvent(event);
	return {
		screenX: event.screenX,
		screenY: event.screenY
	}
}
/* 获取当前日期(yyyy-mm-dd) */
function today(){
	var time = new Date();
	var y = time.getFullYear(),
		m = time.getMonth()+1,
		d = time.getDate();
	m = m<10?0+m.toString():m;
	return y+'-'+m+'-'+d;
}
/*
 * 1.如果当前浏览器支持getElementsByClassName函数，则调用该原生函数；
 * 2.如果当前浏览器支持则使用XPath；//一种浏览器内置的定位XML文档的强大方式，不过浏览器支持方面不统一
 * 3.支持多个类名的搜索，不计先后顺序；
 * 4.返回真正的节点数组，而不是原生的一个nodelist。
 * */
var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
        getElementsByClassName = function (className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
                nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
                returnElements = [],
                current;
            for(var i=0, il=elements.length; i<il; i+=1){
                current = elements[i];
                if(!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = "",
                xhtmlNamespace = "http://www.w3.org/1999/xhtml",
                namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
                returnElements = [],
                elements,
                node;
            for(var j=0, jl=classes.length; j<jl; j+=1){
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = [],
                elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
                current,
                returnElements = [],
                match;
            for(var k=0, kl=classes.length; k<kl; k+=1){
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for(var l=0, ll=elements.length; l<ll; l+=1){
                current = elements[l];
                match = false;
                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};
